package lu.med.praxiswahl.wahlumed.client.rechnungen.op.model;

import java.util.List;
import javax.swing.table.AbstractTableModel;

import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemote;
import lu.med.praxiswahl.wahlumed.model.beans.OPSettlementTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.OPSettlementServiceInt;

public class OPSettlementCodeTableModel extends AbstractTableModel
{

    
    /**
     * 
     */
    private static final long serialVersionUID = -2651871644271632690L;
    
    private ContextFactoryRemote remote;
    private OPSettlementServiceInt service;
    
    private OPSettlementCodeDataSet data;
    
    private int selected = -1;
    
    public OPSettlementCodeTableModel(OPSettlementCodeDataSet data)
    {
	this.data = data;
	remote = new ContextFactoryRemote();
	this.service = remote.getRemoteOPSettlementServiceInt();

    }

    
    @Override
    public int getColumnCount()
    {
	// TODO Auto-generated method stub
	return 3;
    }
    public int getSelected()
    {
	return selected;
    }
    
    public void setSelected(int c)
    {
	selected = c;
    }
    

    @Override
    public int getRowCount()
    {
	
	return data.getDataRow().length;
    }

    @Override
    public Object getValueAt(int row, int column)
    {
	if (data == null)
	{
	    return "";
	}
	return data.getDataRow()[row][column];
    }

    @Override
    public String getColumnName(int columnIndex)
    {
	switch (columnIndex)
	{
	case 0:
	    return "CODE";
	case 1:
	    return "BESCHREIBUNG";
	case 2:
	    return "FAKTOR";
	/*case 3:
	    return "KLASSE";*/
	    
	}
	return "";
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
	return true;
    }
    
    @Override
    public void setValueAt(Object d, int row, int column)
    {
	if (data == null)
	{
	    return;
	}

	data.setDataRow((String) d, row, column);

	this.fireTableDataChanged();
    }
    
    /*
     * Rufe vorhandene OP_Tarife ab
     */
    public void retrieveSettlementCodeFromDB()
    {
	this.data.clearDataSet();
	
	List<OPSettlementTBL> list = this.service.getAvailableOPSettlementCode();
	
	this.data.setDataRow(new String[list.size()][4]) ;
	
	for(int i=0; i<list.size(); i++)
	{
	    this.data.getDataRow()[i][0] = list.get(i).getCode_label();
	    this.data.getDataRow()[i][1] = list.get(i).getDescription();
	    this.data.getDataRow()[i][2] = ""+list.get(i).getFactor();
	    //this.data.getDataRow()[i][3] = ""+list.get(i).getSettlement_class();
	}
	
	this.fireTableDataChanged();
    }

    public void saveSettlementCodeInDB()
    {
	OPSettlementTBL[] items = new OPSettlementTBL[this.data.countFilledRows()];
	
	for(int i=0; i<items.length; i++)
	{
	    items[i] = new OPSettlementTBL();
	    
	    items[i].setCode_label(this.data.getDataRow()[i][0]);
	    items[i].setDescription(this.data.getDataRow()[i][1]);
	    items[i].setFactor(new Double(this.data.getDataRow()[i][2]).floatValue());
	    //items[i].setSettlement_class(new Integer(this.data.getDataRow()[i][3]).intValue());
	    
	   this.service.updateSettlementCode(items[i]);
	}
    }
    
    public void addNewOPSettlementCode(String code)
    {
	this.service.createOPSettlementCode(code, null, 0f);
	this.retrieveSettlementCodeFromDB();
	this.fireTableDataChanged();
    }
    
    public void deleteOPSettlmentCode(String code)
    {
	this.service.deleteOPSettlementCode(code);
	this.retrieveSettlementCodeFromDB();
	this.fireTableDataChanged();
    }
}
